import java.util.Arrays;
import java.util.Scanner;

public class TestDemo {
    public static void main(String[] args) {

        //不规则的数组，省略了列
        int[][] array = new int[2][];
        //在没指定列时，array[0]，array[1]里存的都是null，
        //因为空指针没有指向，所以不能.length来计算大小
        System.out.println(array[0]);
        System.out.println(array[1]);
        //现在分别给array[0],array[1]不同的指向，它们就分别有不同长度的对象了。
        array[0] = new int[3];//3列，长度为3
        array[1] = new int[]{1,2,3,4};//4列，长度为4
        //因为指定了列，所以array[0]和array[1]就有指向了,不在输出null了
        System.out.println(array[0]);
        System.out.println(array[1]);
        //输出不规则二维数组中的数据，这是其中的一种方法，另外两种输出方法也都可以
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j]+" ");
            }
            System.out.println();

        }




    }
    //在二维数组中，与一维数组有些不同
    public static void main11(String[] args) {
        /*二维数组是特殊的一维数组
         这个一维数组的每个元素（行）也是一个一维数组
        */
        int[][] array = {{1,2,3},{4,5,6}};
        //1、数组名.length
        System.out.println(array.length);//行数
        System.out.println(array[0].length);//列数

        //2、Arrays.toString(数组名)与 Arrays.deepToString(数组名)
        System.out.println(Arrays.toString(array));
        //输出的是一维数组的地址，即{1,2,3}和{4,5,6}的地址
        System.out.println(Arrays.deepToString(array));
        //这输出的才是二维数组中的每个元素

    }
    //遍历二维数组
    public static void main12(String[] args) {
        int[][] array = {{1,2,3},{4,5,6}};
        //1、for循环
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j <array[i].length ; j++) {
                System.out.print(array[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println();
        //2、foreach  增强for循环
        /*（数组中每个元素的类型定义的一个变量:数组名）
        * 一维数组中的每个元素也是一个一维数组*/
        for (int[] arr:array) {
            for (int x:arr) {
                System.out.print(x +" ");
            }
            System.out.println();
        }
        System.out.println();
        //3、Arrays.deepToString(数组名)
        System.out.println(Arrays.deepToString(array));
    }
    public static void main10(String[] args) {
        //第一种：只分配了内存，没初始化
        //没初始化，数组中存的是默认值
        int[][] array3 = new int[2][3];

        int[][]array5;
        array5 = new int[2][3];

        //第二种，初始化了
        //定义同时就初始化的，new int[]可省
        int[][] array1 = {{1,2,3},{4,5,6}};

        int[][] array2 = new int[][]{{1,2,3},{4,5,6}};
        //定义后才初始化，分成了两步，new int[]必须有
        int[][] array4;
        array4 = new int[][]{{1,2,3},{4,5,6}};

    }
    //数组排序
    //冒泡排序
    public static void bubbleSort(int[] array){
        //趟数
        for (int i = 0; i < array.length-1; i++) {
            boolean flag = true;
            //交换
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j]>array[j+1]){
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    flag = false;
                }
            }
            if(flag == true) {
                return;
            }
        }
    }
    public static void main9(String[] args) {
        int[] array = {9,8,7,6,5,4,3,2,1,0};
        bubbleSort(array);
        System.out.println(Arrays.toString(array));

    }
    public static void main3(String[] args) {
        int[] array = {2,5,9,26,17,88,47,94,1,6};
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
    }
    public static void main7(String[] args) {
        int[] array = new int[10];
        Arrays.fill(array,2,5,1);//[2,5)
        System.out.println(Arrays.toString(array));
    }
    public static void main6(String[] args) {
        int[] array = {1, 2, 5, 6, 9, 17, 26, 47, 88, 94};
        int[] array1 = {1, 2, 5, 6, 9, 17, 26, 47, 88, 94};
        boolean ret = Arrays.equals(array,array1);
        System.out.println(ret);
    }
    public static void reserve(int[] array){
        int left = 0;
        int right = array.length - 1;
        while(left<right){
            int tmp = array[left];
            array[left] = array[right];
            array[right] = tmp;
            left++;
            right--;
        }
    }
    public static void main5(String[] args) {
        int[] array = {1, 2, 5, 6, 9, 17, 26, 47, 88, 94};
        reserve(array);
        System.out.println(Arrays.toString(array));
    }
    public static void main4(String[] args) {
        int[] array = {1, 2, 5, 6, 9, 17, 26, 47, 88, 94};
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int ret = Arrays.binarySearch(array,k);
        if(ret>=0){
            System.out.println("找到了，下标为："+ret);
        }else{
            System.out.println("找不到");
        }

    }

    //顺序查找
    /*for循环遍历数组，相等返回下标。*/
    public static int binarySearch(int[] array,int k){
        //二分查找
        int left = 0;
        int right = array.length-1;
        while(left<=right){
            int mid = left+((right - left)>>1);
            if(k<array[mid]){
                right = mid - 1;
            }else if(k>array[mid]){
                left = mid +1;
            }else{
                return mid;
            }
        }
        return -1;
    }
    public static void main8(String[] args) {
        int[] array = {1,3,5,7,9,13};
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int ret = binarySearch(array,k);
        if(ret == -1){
            System.out.println("找不到");
        }else{
            System.out.println("找到了，下标为："+ret);
        }
    }
    public static double ave(int[] array){
        int sum = 0;
        for (int x:array) {
            sum += x;
        }
        return sum*1.0/ array.length;
    }
    public static void main1(String[] args) {
        int[] array = {1,2,4,6,8};
        System.out.println(ave(array));
    }
}
